package com.xw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xw.common.Constants;
import com.xw.common.Result;
import com.xw.entity.SubjectCategory;
import com.xw.req.PageRequest;
import com.xw.service.SubjectCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xw
 * &#064;Date  2024/12/5 20:18
 * 课程分类的控制器
 */
@RestController
@RequestMapping("/subjectCategory")
@Api(tags = "课程分类管理")
public class SubjectCategoryController {


    @Resource
    private SubjectCategoryService subjectCategoryService;


    @PostMapping("/create")
    @ApiOperation("新增课程分类")
    public Result<?> publishSubjectCategory(@RequestBody @Validated SubjectCategory subjectCategory) {
        subjectCategoryService.insertSubjectCategory(subjectCategory);
        return new Result<>(Constants.SUCCESS);
    }

    @PostMapping("/update")
    @ApiOperation("修改课程分类")
    public Result<?> updateSubjectCategory(@RequestBody @Validated SubjectCategory subjectCategory) {
        subjectCategoryService.updateSubjectCategory(subjectCategory);
        return new Result<>(Constants.SUCCESS);
    }


    /**
     * 删除课程
     * */
    @DeleteMapping("/{id}")
    @ApiOperation("删除课程分类")
    public Result<?> deleteSubjectCategory(@PathVariable("id") Long id) {
        subjectCategoryService.deleteSubjectCategory(id);
        return new Result<>(Constants.SUCCESS);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取课程分类详情")
    public Result<?> getSubjectCategoryById(@PathVariable("id") Long id) {
        SubjectCategory subjectCategory = subjectCategoryService.getById(id);
        if (subjectCategory != null) {
            return new Result<>(subjectCategory, Constants.SUCCESS);
        } else {
            return new Result<>(Constants.ERROR, "课程分类不存在");
        }
    }

    @GetMapping("/all")
    @ApiOperation("获取所有课程分类列表")
    public Result<?> getAllSubjectCategoryList() {
        List<SubjectCategory> subjectCategory = subjectCategoryService.getAllSubjectCategoryList();
        return new Result<>(subjectCategory, Constants.SUCCESS);
    }


    @PostMapping("page")
    @ApiOperation(value = "分页获取课程分类列表")
    public Result<?> page(@RequestBody PageRequest<SubjectCategory> pageRequest) {
        return new Result<>(subjectCategoryService.page(pageRequest), Constants.SUCCESS);
    }

    @PostMapping("deleteBatchByIds")
    @ApiOperation("通过ID批量删除分类信息")
    public Result<?> deleteBatchByIds(@RequestBody List<Integer> ids) {
        subjectCategoryService.deleteBatchByIds(ids);
        return new Result<>(Constants.SUCCESS);
    }

    @GetMapping("getNewSubjectCategory")
    @ApiOperation("获取最新的四个分类")
    public Result<?> getNewSubjectCategory() {
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.list(
                new LambdaQueryWrapper<SubjectCategory>()
                        .orderByDesc(SubjectCategory::getCreateTime)
                        .last("limit " + 4)
        );
        return new Result<>(subjectCategoryList, Constants.SUCCESS);
    }


}
